package com.ygou.service.impl;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ygou.mapper.GoodsMapper;
import com.ygou.mapper.OrdergoodsMapper;
import com.ygou.mapper.OrdersMapper;
import com.ygou.mapper.RefundsMapper;
import com.ygou.mapper.SkusMapper;
import com.ygou.mapper.UserRanksMapper;
import com.ygou.mapper.UsersMapper;
import com.ygou.pojo.Goods;
import com.ygou.pojo.Ordergoods;
import com.ygou.pojo.OrdergoodsExample;
import com.ygou.pojo.Orders;
import com.ygou.pojo.Refunds;
import com.ygou.pojo.RefundsExample;
import com.ygou.pojo.Skus;
import com.ygou.pojo.SkusExample;
import com.ygou.pojo.UserRanks;
import com.ygou.pojo.Users;
import com.ygou.pojo.RefundsExample.Criteria;
import com.ygou.service.RefundsService;

@Service
public class RefundsServiceImpl implements RefundsService {
	
	@Autowired
	private RefundsMapper refundsMapper;
	@Autowired
	private OrdersMapper ordersMapper;
	@Autowired
	private OrdergoodsMapper ordergoodsMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private SkusMapper skusMapper;
	@Autowired
	private UsersMapper usersMapper;
	@Autowired
	private UserRanksMapper userRanksMapper;
	/**
	 * 根据条件查询所有退款订单列表
	 */
	@Override
	public List<Refunds> selectByExample(String createTime, Integer isRefund) {
		RefundsExample example=new RefundsExample();
		Criteria c=example.createCriteria();
		if(createTime!=null&&!"".equals(createTime)){
			c.andCreatetimeEqualTo(Date.valueOf(createTime));
		}
		if(isRefund!=null&&!"".equals(isRefund)){
			c.andIsrefundEqualTo((byte)isRefund.intValue());
		}
		return refundsMapper.selectByExample(example);
	}

	/**
	 * 根据主键查询退款信息
	 */
	@Override
	public Refunds selectByPrimaryKey(Integer id) {
		return refundsMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 根据条件查询所有退款订单列表
	 */
	@Override
	public List<Refunds> findrefund(String createTime, Integer isRefund,Integer userid) {
		Map<String,Object> map=new HashMap<String,Object>();
		if(createTime!=null&&!"".equals(createTime)){
			map.put("createTime", createTime);
		}
		if(isRefund!=null&&!"".equals(isRefund)){
			map.put("isRefund", isRefund);
		}
		if(userid!=null&&!"".equals(userid)){
			map.put("userid", userid);
		}
		return refundsMapper.findrefund(map);
	}

	/**
	 * 退款 、驳回
	 * @param refund
	 * @return
	 */
	@Override
	public boolean refund(Refunds refund) {
		try {
			Orders order=ordersMapper.selectByPrimaryKey(refund.getOrderid());
			if(refund.getIsrefund()==-1){
				//设置该订单已退款
				order.setIsrefund((byte)0);
				ordersMapper.updateByPrimaryKey(order);
			}
			
			//设置退款信息为（已退款、驳回）
			refundsMapper.updateByPrimaryKeySelective(refund);
			if(refund.getIsrefund()==1){
				//积分
				int score=0;
				//获取该订单下的所有订单商品信息
				OrdergoodsExample oex=new OrdergoodsExample();
				com.ygou.pojo.OrdergoodsExample.Criteria oc=oex.createCriteria();
				oc.andOrderidEqualTo(refund.getOrderid());
				List<Ordergoods> ogs=ordergoodsMapper.selectByExample(oex);
				
				for (Ordergoods ordergoods : ogs) {
					Goods good=goodsMapper.selectByPrimaryKey(ordergoods.getGoodsid());
					//累加商品积分
					score+=good.getGoodsscore()*ordergoods.getGoodnums();
					//增加商品总库存
					good.setStock(good.getStock()+ordergoods.getGoodnums());
					
					//获取sku库存
					SkusExample sexample=new SkusExample();
					com.ygou.pojo.SkusExample.Criteria sc = sexample.createCriteria();
					sc.andGoodsidEqualTo(good.getGoodsid());
					sc.andSkuattrEqualTo(ordergoods.getSkuattr());
					Skus sku=skusMapper.selectByExample(sexample).get(0);
					//增加sku库存
					sku.setStock(sku.getStock()+ordergoods.getGoodnums());
					skusMapper.updateByPrimaryKey(sku);
					//增减少销量
					good.setSale(good.getSale()-1);
					goodsMapper.updateByPrimaryKey(good);
				}
				
				//获取用户信息
				Users user=usersMapper.selectByPrimaryKey(order.getUserid());
				
				//减少用户积分
				user.setUserscore(user.getUserscore()-score);
				
				/**
				 * 获取等级集合 并且根据积分做等级联动
				 */
				List<UserRanks> ranks=userRanksMapper.selectByExample(null);
				for (UserRanks userRanks : ranks) {
					if(user.getUserscore()>=userRanks.getStartscore() && user.getUserscore()<=userRanks.getEndscore()){
						user.setRankid(userRanks.getRankid());
					}
				}
				//修改用户信息
				usersMapper.updateByPrimaryKey(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean insertRefunds(Refunds refund) {
		Orders order=new Orders();
		order.setOrderid(refund.getOrderid());
		order.setIsrefund((byte)1);//正在处理
		ordersMapper.updateByPrimaryKeySelective(order);
		refund.setIsrefund((byte)0);
		refund.setCreatetime(new java.util.Date());
		return refundsMapper.insert(refund)>0;
	}

}
